// pages/yifenmaidaiyan/yifenmaidaiyan.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: true, //节流,

    wangdian: '',
    shangwang: '',
    sbwngdian: true,
    wangdiany: true,
    POST_QX:'',
    L3name:'',
    L4name:'',

    xxkuang: true,
  },


  paaay(){

    this.setData({
      xxkuang: false
    })

  },



  pay: function () {
    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    });
    if (this.data.flag) {
      this.data.flag = false
      if (this.data.L4name == '') {
        wx.showToast({
          title: '请选择市区',
          icon: 'error',
          duration: 2000
        })
      }else{
      console.log(this.data.openid);
      console.log(111);
      // this.gitxin() 
      wx.request({
        url: app.globalData.interface+'/newVersion/API_1070_weiXinPayment',
        method: 'POST',

        data: {
          GOODS_ID: 2000049244,
          REDBACKNOTE: this.data.L3name+'_'+this.data.L4name,
          PAYMENT_AMOUNT: 0.01,
          OPEN_ID: this.data.openid,
        },

        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          this.setData({
            MESSAGE: res.data.MESSAGE,
            ORDER_NUMBER: res.data.ORDER_NUMBER,
            ORDER_PRICE_ZERO: res.data.ORDER_PRICE_ZERO,
            prepay_id: res.data.prepay_id,
            nonceStr: res.data.MESSAGE.nonceStr,
            package: res.data.MESSAGE.package,
            paySign: res.data.MESSAGE.paySign,
            signType: res.data.MESSAGE.signType,
            timeStamp: res.data.MESSAGE.timeStamp,
          })
          console.log(res.data.MESSAGE.nonceStr);
          if (!res.data.MESSAGE.nonceStr || !res.data.MESSAGE.package || !res.data.MESSAGE.paySign) {
            wx.showToast({
              title: '付款失败',
              icon: 'loding',
              duration: 2000
            })
          } else {

            wx.requestPayment({
              'timeStamp': this.data.timeStamp,
              'nonceStr': this.data.nonceStr,
              'package': this.data.package,
              'signType': this.data.signType,
              'paySign': this.data.paySign,
              'success': function (res) {
                console.log('支付返回数据', res)
                // setTimeout(() => {



                wx.showModal({
                  title: '温馨提示',
                  content: '恭喜购买成功！',
                  success: function (res) {
                    if (res.confirm) { //这里是点击了确定以后
                      wx.navigateTo({
                        url: '../orders/orders',
                      })
                    } else { //这里是点击了取消以后
                      console.log('用户点击取消')
                      wx.navigateTo({
                        url: '../orders/orders',
                      })
                    }
                  }
                })
                // }, 1000)
              },
              'fail': function (res) {
                wx.showToast({
                  title: '付款失败',
                  icon: 'error',
                  duration: 2000
                })
                // wx.redirectTo({
                //   url: '../order/order?currentIndex=' + 1 + '&formUrl=' + 1 + '&formOrder=2',
                // })
              },
              // complete: function () {
              //   this.setData({
              //     hiddenLoading: true,
              //     submitOrder: true
              //   })
              // }
            })
            console.log(this.data.timeStamp);
            console.log(this.data.nonceStr);
            console.log(this.data.package);
            console.log(this.data.signType);
            console.log(this.data.paySign);
          }
          // console.log(JSON.stringify(res.data.myPrizeList));
        }
      })
      }
      setTimeout(() => {
        console.log('-----------------', 1);
        this.data.flag = true
      }, 3000)
    }
  },






  wangdianshagn() {

    if (this.data.L3name == '') {
      wx.showToast({
        title: '请先选择县区！',
        icon: 'none',
        duration: 1500
      })
    } else {

      this.setData({
        sbwngdian: false
      })
    }



  },
  wangshang(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.L4name);
    console.log(e.currentTarget.dataset.item.EMPLOYEE_ID);

    this.setData({
      L4name: e.currentTarget.dataset.item.L4name,
      sbwngdian: true,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,


    })
  },



    //网点选择
    wangji() {
      this.setData({
        wangdiany: false
      })
    },
  
    xwang(e) {
      console.log(e);
      console.log(e.currentTarget.dataset.item.wangname);
  
      this.setData({
        L3name: e.currentTarget.dataset.item.L3name,
        POST_ADD: e.currentTarget.dataset.item.newDistrict,
        wangdiany: true,
        L3code: e.currentTarget.dataset.item.L3code,
        POST_QX:'',
      })
      this.regionwangdian()
    },
  
    region() {
      wx.request({
        url: app.globalData.interface+'/ankangdabiao/API_5515_userQueryQuXian',
        method: 'POST',
        data: {
          L2code: 61012777,
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
  
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data.data);
          this.setData({
            wangdian: res.data.data,
          })
   
        }
      })
    },
  
  
  
    regionwangdian() {
      wx.request({
        url: app.globalData.interface+'/ankangdabiao/API_5517_userQueryWangDian',
        method: 'POST',
        data: {
          L3code: this.data.L3code,
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
  
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data.data);
          this.setData({
            shangwang: res.data.data,
          })
  
        }
      })
    },
  














  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.region()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})